/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddDisel_Home.java
 *
 * Created on Oct 2, 2014, 4:03:29 PM
 */
package com.JD.DiselLog;

import com.JD.Disel.Hibernate.Rawdiselupdatelog;
import com.JD.master.Validation.Validator;
import java.util.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author admin
 */
public class AddDisel_Home extends javax.swing.JFrame {

    /** Creates new form AddDisel_Home */
    com.JD.master.Validation.Validator validator = new Validator();
    String rawField1 = "NO";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "NO";
    String currentQuentity = "";
    String addedQuantity = "";
    String totalQuantity = "";
    String openingQuantity = "";
    String currentDate = "";

    public AddDisel_Home() {
        initComponents();








        SessionFactory sessionFactory = com.JD.Disel.Hibernate.DiselLog_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        Query query = session.createQuery("from com.JD.Disel.Hibernate.Currentdisellog");

        List list = query.list();

        for (Object object : list) {

            com.JD.Disel.Hibernate.Currentdisellog currentdisellog = (com.JD.Disel.Hibernate.Currentdisellog) object;

            showCurrentQuantityOfDisel_Lable.setText(currentdisellog.getCurrentQuantity());
            currentQuentity = currentdisellog.getCurrentQuantity();
            openingQuantity = currentdisellog.getOpeningQuantity();
            currentDate = currentdisellog.getCurrentDate();


        }




    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addDisel_Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        partyTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        showCurrentQuantityOfDisel_Lable = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        addDisel_TextField = new javax.swing.JTextField();
        addDataToDatabase_Button = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        addedBy_TextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        addedFrom_TextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addDisel_Panel.setBackground(new java.awt.Color(255, 102, 51));
        addDisel_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        partyTable.setFont(new java.awt.Font("Traditional Arabic", 0, 14));
        partyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Current Quantity", "Addes Quantity", "Added By", "Added From", "Added On Date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(partyTable);

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel1.setText("Current Quantity Of Disel:");

        showCurrentQuantityOfDisel_Lable.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        showCurrentQuantityOfDisel_Lable.setText("jLabel2");

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel2.setText("Add Disel:");

        addDisel_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 18));

        addDataToDatabase_Button.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        addDataToDatabase_Button.setText("Update Quantity Of Disel");
        addDataToDatabase_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDataToDatabase_ButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel3.setText("Added By:");

        addedBy_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        addedBy_TextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                addedBy_TextFieldKeyReleased(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel4.setText("Added From:");

        addedFrom_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 18));

        javax.swing.GroupLayout addDisel_PanelLayout = new javax.swing.GroupLayout(addDisel_Panel);
        addDisel_Panel.setLayout(addDisel_PanelLayout);
        addDisel_PanelLayout.setHorizontalGroup(
            addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addDisel_PanelLayout.createSequentialGroup()
                .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1050, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(addDisel_PanelLayout.createSequentialGroup()
                        .addGap(194, 194, 194)
                        .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(addDisel_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                                .addComponent(addedBy_TextField)
                                .addComponent(addedFrom_TextField)
                                .addComponent(addDataToDatabase_Button))
                            .addGroup(addDisel_PanelLayout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addComponent(showCurrentQuantityOfDisel_Lable, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        addDisel_PanelLayout.setVerticalGroup(
            addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addDisel_PanelLayout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(showCurrentQuantityOfDisel_Lable))
                .addGap(32, 32, 32)
                .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(addDisel_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addedBy_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(26, 26, 26)
                .addGroup(addDisel_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addedFrom_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(68, 68, 68)
                .addComponent(addDataToDatabase_Button)
                .addGap(82, 82, 82)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addDisel_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 1175, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addDisel_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 718, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addDataToDatabase_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDataToDatabase_ButtonActionPerformed
        // TODO add your handling code here:

        addedQuantity = addDisel_TextField.getText();

        totalQuantity = Integer.parseInt(currentQuentity) + Integer.parseInt(addedQuantity) + "";

        String dateOfAddition = "09/10/2014";
        String timeOfAddition = "10:18 AM";
        String addedBy = addedBy_TextField.getText();
        String addedFromSource = addedFrom_TextField.getText();


        SessionFactory sessionFactory = com.JD.Disel.Hibernate.DiselLog_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        com.JD.Disel.Hibernate.Rawdiselupdatelog rawdiselupdatelog = new Rawdiselupdatelog(currentQuentity, addedQuantity, totalQuantity, dateOfAddition, timeOfAddition, addedBy, addedFromSource, rawField1, rawField2, rawField3, rawField4);

        session.save(rawdiselupdatelog);
        transaction.commit();


        new AddDataToDataBase(totalQuantity);


        addDisel_TextField.setText("");
        addedBy_TextField.setText("");
        addedFrom_TextField.setText("");

        showCurrentQuantityOfDisel_Lable.setText(totalQuantity);






    }//GEN-LAST:event_addDataToDatabase_ButtonActionPerformed

    private void addedBy_TextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addedBy_TextFieldKeyReleased
        // TODO add your handling code here:

        addedBy_TextField.setText(validator.stringValidator(addedBy_TextField.getText()));

    }//GEN-LAST:event_addedBy_TextFieldKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDataToDatabase_Button;
    public javax.swing.JPanel addDisel_Panel;
    private javax.swing.JTextField addDisel_TextField;
    private javax.swing.JTextField addedBy_TextField;
    private javax.swing.JTextField addedFrom_TextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable partyTable;
    public javax.swing.JLabel showCurrentQuantityOfDisel_Lable;
    // End of variables declaration//GEN-END:variables

    public JLabel getShowCurrentQuantityOfDisel_Lable() {
        return showCurrentQuantityOfDisel_Lable;
    }
    // End of variables declaration

    public JPanel getAddDisel_Panel() {
        return addDisel_Panel;
    }
}
